package com.alangyun.remind.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alangyun.remind.error.MindException;
import com.alangyun.remind.repository.data.OrganCaution;
import com.alangyun.remind.service.IOrganService;
import com.alangyun.remind.service.IUploadService;
import com.alangyun.remind.vo.DataResponse;
import com.alangyun.remind.vo.ErrorCode;
import com.alangyun.remind.vo.Response;

/**
 * 劳动组织模块
 *
 * @class com.alangyun.remind.controller.OrganController
 * @company 北京诚朗信息技术有限公司
 * @author hoobort klxukun@126.com
 * @datetime 2022年11月10日 下午1:32:51
 *
 */
@Controller
public class OrganController extends AbstractController {

	@Resource
	private IUploadService svcUpload;
	@Resource
	private IOrganService svcData;

	/**
	 * 专业部门及班组缺冗员预警
	 *
	 * @author hoobort klxukun@126.com
	 * @company 北京诚朗信息技术有限公司
	 * @datetime 2022年11月10日 下午1:32:46
	 * @return
	 */
	@RequestMapping(value = Paths.OrganUrl.ORGAN_TEAM, method = { RequestMethod.GET, RequestMethod.POST })
	public String teamPage(Model model) {
		String ret = "/mind/organ-analysis";
		try {
			validateTimeout();
			
			model.addAttribute(CategoryIdentities.VARIABLE, CategoryIdentities.TISSUE_ID);
			model.addAttribute("list", svcData.listNames());
		} catch (MindException me) {
			ret = pageError(model, me.getMessage());
			e(me.getMessage(), me);
		} catch (Exception ex) {
			ret = pageError(model, "加载劳动组织部门机构信息失败，请确认是否已经上传");
			e("专业部门及班组缺冗员预警页面出错", ex);
		}

		return ret;
	}

	@PostMapping(value = Paths.OrganUrl.ORGAN_UPLOAD)
	@ResponseBody
	public Response teamUpload(@RequestParam("file") MultipartFile file) {
		Response ret = new Response();
		try {
			validateTimeout();
			
			svcUpload.parseOrganFile(file);
			ret.setCode(ErrorCode.SUCCESS);
		} catch (MindException me) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc(me.getMessage());
			e(me.getMessage(), me);
		} catch (Exception ex) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc("上传组织机构数据预警数据处理失败");
			e("上传组织机构数据预警数据处理失败", ex);
		}
		return ret;
	}

	@PostMapping(value = Paths.OrganUrl.ORGAN_LIST)
	@ResponseBody
	public DataResponse<List<OrganCaution>> listCautionBySize(
			@RequestParam(value = "size", defaultValue = "15") int size) {
		DataResponse<List<OrganCaution>> ret = new DataResponse<List<OrganCaution>>();
		try {
			validateTimeout();
			
			ret.setData(svcData.listCaution(size)).setCode(ErrorCode.SUCCESS);
		} catch (MindException me) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc(me.getMessage());
			e(me.getMessage(), me);
		} catch (Exception ex) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc("上传组织机构数据预警数据处理失败");
			e("上传组织机构数据预警数据处理失败", ex);
		}
		return ret;
	}

	@PostMapping(value = Paths.OrganUrl.ORGAN_QUERY)
	@ResponseBody
	public DataResponse<List<OrganCaution>> queryCautions(@RequestParam("ids") Integer[] data) {
		DataResponse<List<OrganCaution>> ret = new DataResponse<List<OrganCaution>>();
		try {
			validateTimeout();
			
			ret.setData(svcData.listCaution(Arrays.asList(data))).setCode(ErrorCode.SUCCESS);
		} catch (MindException me) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc(me.getMessage());
			e(me.getMessage(), me);
		} catch (Exception ex) {
			ret.setCode(ErrorCode.EXCEPTION).setDesc("上传组织机构数据预警数据处理失败");
			e("上传组织机构数据预警数据处理失败", ex);
		}

		return ret;
	}
}
